// miniprogram/pages/mall/home/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    noMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.catalog) {
      this.catalog = options.catalog;
    }
    this.init();
  },
  init() {
    this.no = 1;
    var params = {};
    params.no = this.no;
    params.size = 10;
    params.auditState = "checked";
    if (this.catalog) {
      params.shopCatalog = this.catalog;
    }
    app.dataRequest("/rest/couponrule/searchByReceive.htm", params, this, "coupons");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.info(this.totalPage);
    if (this.no < this.totalPage) {
      this.no = this.no + 1;
      this.setData({
        noMore: false
      });
      var params = {};
      params.no = this.no;
      params.size = 10;
      params.auditState = "checked";
      if (this.catalog) {
        params.shopCatalog = this.catalog;
      }


      app.loadMore("/rest/couponrule/searchByReceive.htm", params, this, "coupons");

    } else {
      this.setData({
        noMore: true
      });
    }
  },
  receiveCoupon(event) {
    console.info(event);
    var obj = event.currentTarget.dataset.obj;
    var param = {};
    param.couponRule = obj.id;
    app.postRequest("/rest/coupon/create.htm", param, this.backCreate);
  },
  backCreate(res) {
    if (res.code == 0) {
      app.showModal("领券成功");
    } else {
      app.showModal(res.msg);
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})